﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;

namespace JunQuanPrinter.DB
{
    class CConfigDB
    {
        private static CConfigDB m_Obj = null;
        private CSQLite m_Sqlite = CSQLite.getClass();
        public const Int32 C_idx_Com = 1;       //串口号
        public const Int32 C_idx_Baud = 2;      //L
        public const Int32 C_idx_LastProNo = 3;  //最近一次产品卡号
        public const Int32 C_idx_LastLineNum = 4; //最近一次结束代码
        public const Int32 C_idx_Inteval = 5;   //喷码间隔
        public const Int32 C_idx_Font_Fx = 6;   //字体反向
        public static CConfigDB getClass()
        {
            if(m_Obj == null)
            {
                m_Obj = new CConfigDB();
            }
            return m_Obj;
        }


        /// <summary>
        /// 获取列表数据
        /// </summary>
        /// <param name="strFilter"></param>
        /// <returns></returns>
        public DataTable getList(String strFilter)
        {
            String strSQL = " Select * from tConfig where 1=1 {0} ";
            strSQL = String.Format(strSQL, strFilter);
            return m_Sqlite.ExecuteQueryTable(strSQL);
        }


        /// <summary>
        /// 插入新记录
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public Boolean Add(Int32 idx, String Value)
        {
            String strSQL = @" Insert into tConfig(idx, Value) Values({0}, '{1}') ";
            strSQL = String.Format(strSQL, idx, Value);
            int iCount = m_Sqlite.ExecuteNonQuery(strSQL);
            return true;
        }


        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="idx"></param>
        /// <param name="Value"></param>
        /// <returns></returns>
        public Boolean Upd(Int32 idx, String Value)
        {
            String strSQL = @" Update tConfig set Value = '{0}' where idx = {1} ";
            strSQL = String.Format(strSQL, Value, idx );
            int iCount = m_Sqlite.ExecuteNonQuery(strSQL);
            if(iCount == 0)
            {
                Add(idx, Value);
            }
            return true;
        }


        /// <summary>
        /// 获取值
        /// </summary>
        /// <param name="idx"></param>
        /// <returns></returns>
        public String getValue(Int32 idx)
        {
            String strFilter = " and idx = {0} ";
            strFilter = String.Format(strFilter, idx);
            DataTable dt = getList(strFilter);
            if (dt.Rows.Count == 0)
                return "";
            DataRow dr = dt.Rows[0];
            return Convert.ToString(dr["Value"]);
        }


    }
}
